
import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, getUserInfo, getUserDetailById } from '@/api/user'
import { resetRouter } from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    updateToken(state, token) {
      state.token = token
      // 存储到本地 Cookie
      // 按需导入后直接调用setToken()
      setToken(token)
    },
    getuserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    removeToken(state) {
      state.token = ''
      removeToken()
    },
    removeUserInfo(state) {
      state.userInfo = {}
    }

  },
  actions: {
    async  userLogin(context, loginForm) {
      const res = await login(loginForm)
      context.commit('updateToken', res.data)
      // 返回出去
      return res
    },
    async  getuserinfo(context) {
      const res = await getUserInfo()
      console.log(res.data)
      const imgs = await getUserDetailById(res.data.userId)
      console.log(imgs.data)
      context.commit('getuserInfo', { ...res.data, ...imgs.data })
      // return { ...res.data, ...imgs.data }
    },
    logout(context) {
      context.commit('removeToken')
      context.commit('removeUserInfo')
      // 重置路由
      resetRouter()
    }
  }
  // actions: {
  //   async  userLogin(context, loginForm) {
  //     const res = await login(loginForm)
  //     context.commit('updateToken', res.data)
  //     return res
  //   }
  // }
}

